package com.wn55.hrms.web.controller;



import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.Permission;
import com.wn55.hrms.service.PermissionService;
import com.wn55.hrms.service.vo.PermissionVo;
import com.wn55.hrms.util.ResultResponse;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @author HP
 */
@RestController
@RequestMapping("permission")
@Api(value = "此接口用于操作Permission功能")
public class PermissionController {

    @Autowired
    PermissionService psi;

    @GetMapping("/listAllMenu")
    @Cacheable(value = "permissionData",keyGenerator = "keyGenerator")
    public ResultResponse<List<PermissionVo>> initAllMenu() {
        ResultResponse<List<PermissionVo>> result = null;
        try {
            List<PermissionVo> permissionVos = psi.initPermission();
            result = new ResultResponse<>(200, "加载菜单成功", permissionVos);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "加载菜单失败");
        }
        return result;
    }

    @GetMapping("/searchRoleMenu/{name}")
    public ResultResponse<List<PermissionVo>> searchRoleMenu(@PathVariable("name")String name) {
        ResultResponse<List<PermissionVo>> result = null;
        try {
            List<PermissionVo> permission = psi.searchRoleMenu(name);
            result = new ResultResponse<>(200, "加载菜单成功", permission);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "加载菜单失败");
        }
        return result;
    }

    @RequestMapping("/listPermission")
    @Cacheable(value = "permissionPagesData",keyGenerator = "keyGenerator")

    public ResultResponse<PageInfo<Permission>> listPermission(Integer currentPage, Integer pageSize, String name) {
        ResultResponse<PageInfo<Permission>> resultResponse;
        try {
            PageInfo<Permission> list = psi.listPermission(currentPage,pageSize,name);
            resultResponse = new ResultResponse<>(200, "加载父权限列表成功！", list);
        } catch (Exception e) {
            e.printStackTrace();
            resultResponse = new ResultResponse<>(201, "加载父权限列表出错！");
        }

        return resultResponse;
    }

    @RequestMapping("/loadParentMenu")
    @Cacheable(value = "menuData",keyGenerator = "keyGenerator")
    public ResultResponse<List<Permission>> loadParentMenu() {
        ResultResponse<List<Permission>> resultResponse;
        try {
            List<Permission> list = psi.loadParentMenu();
            resultResponse = new ResultResponse<>(200, "加载父权限列表成功！", list);
        } catch (Exception e) {
            e.printStackTrace();
            resultResponse = new ResultResponse<>(201, "加载父权限列表出错！");
        }

        return resultResponse;
    }

    @RequestMapping("/updatePermission")
    public ResultResponse<Void> updatePermission(@RequestBody Permission permission) {
        ResultResponse<Void> resultResponse;
        try {
            int i= psi.updatePermission(permission);
            if (i==0){
                resultResponse = new ResultResponse<>(202, "修改权限列表失败！");

            }else {
                resultResponse = new ResultResponse<>(200, "修改权限列表成功！");

            }
        } catch (Exception e) {
            e.printStackTrace();
            resultResponse = new ResultResponse<>(201, "修改权限列表出错！");
        }

        return resultResponse;
    }

    @RequestMapping("/addPermission")
    public ResultResponse<Void> addPermission(@RequestBody Permission permission) {
        ResultResponse<Void> resultResponse;
        try {
            int i=  psi.addPermission(permission);
            if (i==0){
                resultResponse = new ResultResponse<>(202, "添加权限列表失败！");
            }else {
                resultResponse = new ResultResponse<>(200, "添加权限列表成功！");

            }
        } catch (Exception e) {
            e.printStackTrace();
            resultResponse = new ResultResponse<>(201, "添加权限列表出错！");
        }

        return resultResponse;
    }

    @DeleteMapping("/deletePermission")
    public ResultResponse<Void> addPermission(Long id) {
        ResultResponse<Void> resultResponse;
        try {
            int i=  psi.deletePermission(id);
            if (i==0){
                resultResponse = new ResultResponse<>(202, "删除权限列表失败！");
            }else {
                resultResponse = new ResultResponse<>(200, "删除权限列表成功！");

            }
        } catch (Exception e) {
            e.printStackTrace();
            resultResponse = new ResultResponse<>(201, "删除权限列表出错！");
        }

        return resultResponse;
    }

}
